package com.xzp.yygh.user.api;

import com.xzp.yygh.common.handler.AuthContextHandler;
import com.xzp.yygh.common.helper.HttpRequestHelper;
import com.xzp.yygh.common.result.Result;
import com.xzp.yygh.common.util.IpUtil;
import com.xzp.yygh.model.user.UserInfo;
import com.xzp.yygh.user.service.UserInfoService;
import com.xzp.yygh.vo.user.LoginVo;
import com.xzp.yygh.vo.user.UserAuthVo;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


import javax.servlet.http.HttpServletRequest;
import java.util.Map;

@RestController
@RequestMapping("/api/user")
public class UserInfoApiController {
    @Autowired
    private UserInfoService userInfoService;

    @ApiOperation(value = "会员登录")
    @PostMapping("login")
    public Result login(@RequestBody LoginVo loginVo, HttpServletRequest request) {
        // 工具类获取用户登录的ip地址
        String ipAddr = IpUtil.getIpAddr(request);
        loginVo.setIp(ipAddr);
        // 封装为map返回
        Map<String, Object> map = userInfoService.login(loginVo);
        return Result.ok(map);
    }

    @ApiOperation(value = "实名认证")
    @PostMapping("auth/userAuth")
    public Result userAuth(@RequestBody UserAuthVo userAuthVo, HttpServletRequest request){
        // 对用户的认证信息进行更新
        userInfoService.userAuth(AuthContextHandler.getUserId(request), userAuthVo);
        return Result.ok();
    }

    @ApiOperation(value = "获取用户信息")
    @GetMapping("auth/getUserInfo")
    public Result getUserInfo(HttpServletRequest request){
        Long userId = AuthContextHandler.getUserId(request);
        UserInfo userInfo = userInfoService.getById(userId);
        return Result.ok(userInfo);

    }




}
